<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增农合机构管理')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-rural-add">
            <div class="form-group">
                <label class="col-sm-3 control-label">区域编码：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input id="treeId" name="areaId" type="hidden" th:value="${sysArea?.areaId}"/>
                        <input class="form-control" type="text" onclick="selectAreaTree()" id="treeName" readonly="true" th:value="${sysArea?.areaName}">
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">机构编码：</label>
                <div class="col-sm-8">
                    <input name="ruralId" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">机构名称：</label>
                <div class="col-sm-8">
                    <input id="ruralName" name="ruralName" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">级别：</label>
                <div class="col-sm-8">
                    <input id="ruralRank" name="ruralRank" readonly class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "system/rural"
        $("#form-rural-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($("#ruralRank").val() == "") {
                $.modal.alertWarning("农合经办点的只属于乡镇级以上行政区域，请重新选择");
            } else {
                let ruralName = $("#ruralName").val();
                $.ajax ({
                    url: '/system/rural/listAjax',
                    type: 'post',
                    dataType: 'json',
                    data: {"ruralName": ruralName},
                    success: function(data) {
                        if (data.res == 'success') {
                            if ($.validate.form()) {
                                $.operate.save(prefix + "/add", $('#form-rural-add').serialize());
                            }
                        } else {
                            $.modal.msgError("农合机构名称重复！");
                        }
                    }
                });
            }

        }

        /*行政区域管理-新增-选择行政区域树*/
        function selectAreaTree() {
            var options = {
                title: '行政区域选择',
                width: "380",
                url: "/system/area/selectAreaTree/" + $("#treeId").val(),
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }

        /*行政区域树的回调函数*/
        function doSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            $("#areaId").val(body.find('#treeId').val());
            let id = body.find('#treeId').val();
            idLength(id.length)
            layer.close(index);
        }

        /*根据父级区域编码长度决定级别*/
        function idLength(index) {
            if(index == 6){
                $("#ruralRank").val(1);
            }else if (index == 8){
                $("#ruralRank").val(2);
            }else if (index == 10){
                $.modal.alertWarning("农合经办点的只属于乡镇级以上行政区域，请重新选择");
                $("#ruralRank").val("");
            }else if (index == 12){
                $.modal.alertWarning("农合经办点的只属于乡镇级以上行政区域，请重新选择");
                $("#ruralRank").val("");
            }else {
                $("#ruralRank").val("");
            }
        }


    </script>
</body>
</html>
